using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using MM.Entities;

namespace MM.Business
{
    class clsCHI_TIET_CD_Q
    {
        public clsCHI_TIET_CD_Q(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<CHI_TIET_CD_Q> DataTableToLstObj(DataTable dt)
        {
            CHI_TIET_CD_Q chiTietCdQDTO;
            List<CHI_TIET_CD_Q> lstchiTietCdQDTO = new List<CHI_TIET_CD_Q>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                chiTietCdQDTO = new CHI_TIET_CD_Q();
                try
                {
                    chiTietCdQDTO._maChiTiet = int.Parse(dt.Rows[i]["MaChiTiet"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdQDTO._maChucDanh = int.Parse(dt.Rows[i]["MaChucDanh"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdQDTO._tenForm = dt.Rows[i]["TenForm"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstchiTietCdQDTO.Add(chiTietCdQDTO);
            }
            return lstchiTietCdQDTO;
        }
        public CHI_TIET_CD_Q DataTableToObj(DataTable dt)
        {
            CHI_TIET_CD_Q chiTietCdQDTO = new CHI_TIET_CD_Q();
            try
            {
                chiTietCdQDTO._maChiTiet = int.Parse(dt.Rows[0]["MaChiTiet"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdQDTO._maChucDanh = int.Parse(dt.Rows[0]["MaChucDanh"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdQDTO._tenForm = dt.Rows[0]["TenForm"].ToString();
            }
            catch (Exception ex)
            {
            }
            return chiTietCdQDTO;
        }
        public List<CHI_TIET_CD_Q> LoadCHI_TIET_CD_QByMaCD(int maCD)
        {
            DataTable dt = new DataTable();
            List<CHI_TIET_CD_Q> ctCdQDTO = new List<CHI_TIET_CD_Q>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectCHI_TIET_CD_QByMaCD";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChucDanh", maCD);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                ctCdQDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return ctCdQDTO;
        }

        public int InsertUpdateCHI_TIET_CD_Q(int maCd, string tenFrom)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateCHI_TIET_CD_Q";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@TenForm", tenFrom);
                par[1] = new SqlParameter("@MaChucDanh", maCd);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }

        public int UpdateHuyQuyen(int maCd, string tenFrom)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "sp_UpdateHuyQuyen";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@TenForm", tenFrom);
                par[1] = new SqlParameter("@MaChucDanh", maCd);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
